<template>
    <div class="continer" v-loading="upimgLoding">
        <div class="topbox">
            <div>
                <el-button icon="el-icon-refresh" @click="()=>{getList()}">刷新列表</el-button>
                <el-button style="margin-left: 40px;" @click="tuisongMessage">发送推送消息</el-button>
            </div>
            <el-form :model="search" :inline="true" ref="sc" @submit.native.prevent>
                <el-form-item label="" prop="account">
                    <el-input type="text" v-model="search.account" placeholder="请输入账号"></el-input>
                </el-form-item>
                <el-button type="primary" class="ml_10" @click="()=>{getList()}" native-type="submit">搜索</el-button>
                <el-button @click="resetForm('sc')">重置</el-button>
            </el-form>
        </div>
        <el-table ref="tables" v-loading="tableload" :data="listData" style="width:100%" row-key="id" default-expand-all stripe border>
            <el-table-column label="账号" prop="account"></el-table-column>
            <el-table-column label="昵称" >
                <template slot-scope="scope">
                    <div>{{scope.row.nickname || '--'}}</div>
                </template>
            </el-table-column>
            <el-table-column label="类型" prop="type_value"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="()=>{detail(scope.row, 1)}">查看</el-button>
                    <el-button type="text" size="mini" @click="()=>{detail(scope.row, 0)}">删除</el-button>
                    <el-button type="text" size="mini" @click="()=>{xinxituisongList(scope.row)}">信息推送列表</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            layout="prev, pager, next, jumper"
            :total="listLen"
            :page-size="limit"
            @current-change="toPage"
            :current-page.sync="currentPage"
            class="txt_r mt_20"
        ></el-pagination>
        <!-- 表单 -->
        <el-dialog title="详情" :visible.sync="showDetail" :close-on-click-modal="false">
            <div class="fs_18 c_33">用户信息：</div>
            <table class="detail">
                <tr><td class="txt_r">头像：</td><td><img :src='BASE_URL+"/"+avatar'/></td></tr>
                <tr><td class="txt_r">账号：</td><td>{{detailData.account}}</td></tr>
                <tr><td class="txt_r">昵称：</td><td>{{detailData.nickname || '--'}}</td></tr>
                <tr><td class="txt_r">手机号：</td><td>{{detailData.tel || '--'}}</td></tr>
                <!-- <tr><td class="txt_r">公司名称：</td><td>{{detailData.company || '--'}}</td></tr> -->
                <!-- <tr><td class="txt_r">所属行业：</td><td>{{detailData.industry_name || '--'}}</td></tr> -->
                <tr><td class="txt_r">用户类型：</td><td>{{detailData.type_value}}</td></tr>
            </table>
            <div class="mt_26 fs_18 c_33" v-if="detailData.company2">企业信息<span class="fs_16 c_66"> （{{detailData.company2.status_value}}）</span>：</div>
            <el-table ref="tables" v-loading="tableload" :data="userDataList" style="width:100%" row-key="id" default-expand-all stripe border>
                <el-table-column label="公司名称" prop="name"></el-table-column>
                <el-table-column label="地址" prop="addr"></el-table-column>
                <el-table-column label="法定代表人" prop="law_person"></el-table-column>
                <el-table-column label="联系人" prop="linkman"></el-table-column>
                <el-table-column label="联系电话" prop="tel"></el-table-column>
                <el-table-column label="审核状态" prop="status"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row && scope.row.status == '未审核'" type="text" size="mini" @click="()=>{company(scope.row)}">企业审核</el-button>
                        <el-button type="text" size="mini" @click="()=>{userCompanyDetail(scope.row)}">查看</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
        <!-- 查看该账号下所有信息 -->
        <el-dialog title="详情" :visible.sync="userShowDetail" :close-on-click-modal="false">
            <div class="fs_18 c_33">企业信息：</div>
            <table class="detail" v-if="userDetailData">
                <tr><td class="txt_r">公司名称：</td><td>{{userDetailData.name || '--'}}</td></tr>
                <tr><td class="txt_r">营业执照代码：</td><td>{{userDetailData.code || '--'}}</td></tr>
                <tr><td class="txt_r">公司类型：</td><td>{{userDetailData.type || '--'}}</td></tr>
                <tr><td class="txt_r">所属行业：</td><td>{{userDetailData.industry_name || '--'}}</td></tr>
                <tr><td class="txt_r">注册资本(万元)：</td><td>{{userDetailData.registered_assets || '--'}}</td></tr>
                <tr><td class="txt_r">注册时间：</td><td>{{userDetailData.register_time || '--'}}</td></tr>
                <tr><td class="txt_r">注册地址：</td><td>{{userDetailData.addr || '--'}}</td></tr>
                <tr><td class="txt_r">法定代表人：</td><td>{{userDetailData.law_person || '--'}}</td></tr>
                <tr><td class="txt_r">联系电话：</td><td>{{userDetailData.tel || '--'}}</td></tr>
                <tr v-if="userDetailData.fax"><td class="txt_r">传真：</td><td>{{userDetailData.fax || '--'}}</td></tr>
                <tr v-if="userDetailData.email"><td class="txt_r">邮箱：</td><td>{{userDetailData.email || '--'}}</td></tr>
            </table>
        </el-dialog>
        <!-- 审核 -->
        <el-dialog title="企业审核" :visible.sync="showAuthCompany">
            <table class="detailTable" v-if="detailData">
                <tr><td class="txt_r">公司名称：</td><td>{{detailData.name || '--'}}</td></tr>
                <tr><td class="txt_r">营业执照代码：</td><td>{{detailData.code || '--'}}</td></tr>
               <tr><td class="txt_r">公司类型：</td><td>{{detailData.type || '--'}}</td></tr>
                <tr><td class="txt_r">所属行业：</td><td>{{detailData.industry_name || '--'}}</td></tr>
                <tr><td class="txt_r">注册资本(万元)：</td><td>{{detailData.registered_assets || '--'}}</td></tr>
                <tr><td class="txt_r">注册时间：</td><td>{{detailData.register_time || '--'}}</td></tr>
                <tr><td class="txt_r">注册地址：</td><td>{{detailData.addr || '--'}}</td></tr>
                <tr><td class="txt_r">法定代表人：</td><td>{{detailData.law_person || '--'}}</td></tr>
                <tr><td class="txt_r">联系电话：</td><td>{{detailData.tel || '--'}}</td></tr>
                <tr v-if="detailData.fax"><td class="txt_r">传真：</td><td>{{detailData.fax || '--'}}</td></tr>
                <tr v-if="detailData.email"><td class="txt_r">邮箱：</td><td>{{detailData.email || '--'}}</td></tr>
            </table>
            <el-form
                :model="authCompany"
                label-position="right"
                :rules="formRules"
                ref="sh"
                label-width="120px"
              >
                <el-form-item label="是否通过：" prop="status">
                    <el-radio-group v-model="authCompany.status">
                        <el-radio :label="1">通过</el-radio>
                        <el-radio :label="2">不通过</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="理由：" prop="reason">
                    <el-input type="textarea" v-model="authCompany.reason" rows="4"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="hideForm('sh','showAuthCompany')">取 消</el-button>
                <el-button type="primary" @click="submitCompany(detailData.user_id)">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 推送消息 -->
        <el-dialog title="推送消息" :visible.sync="showTuisong" width="85%">
            <el-form
                :model="tuisongForm"
                label-position="right"
                :rules="formRules"
                ref="tuisongForm"
                label-width="80px"
            >
                <el-form-item label="标题" prop="title">
                    <el-input type="text" v-model="tuisongForm.title" placeholder="请输入文章标题"></el-input>
                </el-form-item>
                <el-form-item label="消息类型" prop="category">
                    <el-radio v-model="tuisongForm.category" label="1">系统消息</el-radio>
                    <el-radio v-model="tuisongForm.category" label="2">其他消息</el-radio>
                </el-form-item>
                <el-form-item label="选择用户" prop="selectStr">
                    <el-radio-group v-model="tuisongForm.selectStr">
                        <el-radio label="1" @click.native.prevent='chooseUserRadio(1)'>选择全部用户</el-radio>
                        <el-radio label="2" @click.native.prevent='chooseUserRadio(2)'>选择部分用户</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="内容" prop="content">
                    <div class="quill_pb">
                        <vue-ueditor-wrap v-model="tuisongForm.content" :config="ueConfig"></vue-ueditor-wrap>
                    </div>
                    <el-upload
                        class="dn"
                        :action="BASE_URL+'/admin/file/upload/createfile'"
                        name="file"
                        :before-upload="upimgBefore"
                        :on-success="upimgSuccess"
                        :on-error="upimgError"
                        >
                        <el-button size="small" type="primary" ref="btn">点击上传</el-button>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addTuisongQuxiao">取 消</el-button>
                <el-button type="primary" @click="addTuisong">发 布</el-button>
            </div>
        </el-dialog>
        <!-- 选择用户 -->
        <el-dialog title="选择用户" :visible.sync="showChooseUser" width="85%">
            <el-table ref="tables" v-loading="tableload" :data="listAllData" style="width:100%" default-expand-all stripe border
              :row-key="getRowkeys"
              @selection-change="handleSelectionChange"
              >
                <el-table-column type="selection" :reserve-selection="true" width="55"> </el-table-column>
                <el-table-column label="账号" prop="account"></el-table-column>
                <el-table-column label="名称" prop="nickname"></el-table-column>
                <el-table-column label="类型" prop="type_value"></el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button @click="showChooseUser = false">取 消</el-button>
                <el-button type="primary" @click="quedingUser">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 用户推送列表 -->
        <el-dialog title="用户推送列表" :visible.sync="userTSlistFlag" width="85%">
            <el-table ref="tables" v-loading="tableload" :data="userTSlist" style="width:100%" default-expand-all stripe border row-key="id">
                <el-table-column label="id" prop="id"></el-table-column>
                <el-table-column label="标题" prop="message.title"></el-table-column>
                <el-table-column label="阅读时间" prop="read_time"></el-table-column>
                <el-table-column label="阅读状态" prop="status"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="()=>{xiaoxiDetail(scope.row)}">查看消息详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="userTSlistFlag = false">关 闭</el-button>
            </div>
        </el-dialog>
        <!-- 消息列表详情 -->
        <el-dialog title="用户推送列表" :visible.sync="userTSlistDetailFlag" width="60%">
            <table class="detail">
                <tr><td class="txt_r">标题：</td><td>{{userTSlistDetail.title || '--'}}</td></tr>
                <tr><td class="txt_r">消息类型：</td><td>{{userTSlistDetail.category || '--'}}</td></tr>
                <tr><td class="txt_r">发送用户：</td><td>{{userTSlistDetail.scope || '--'}}</td></tr>
                <tr><td class="txt_r">时间：</td><td>{{userTSlistDetail.create_time || '--'}}</td></tr>
                <tr><td class="txt_r">更新时间：</td><td>{{userTSlistDetail.update_time || '--'}}</td></tr>
                <tr>
                    <td class="txt_r">消息内容：</td>
                    <td>
                        <div class="cont ql-editor mt_20" v-html="userTSlistDetail.content"></div>
                    </td>
                </tr>
            </table>
        </el-dialog>
    </div>
</template>

<script>
import { BASE_URL } from "../../config/app";
import VueUeditorWrap from 'vue-ueditor-wrap';
import * as apis from "../../api/user/user";

export default {
    data() {
        return {
            tableload:false,
            upimgLoding:false,
            listData:[],
            search:{account:""},
            // 分页
            listLen: 0,
            limit: 6,
            currentPage: 1,
            showDetail: false,
            detailData: {},
            avatar: '',
            // 账号下所有信息
            userDataList: [],
            userDetailData: {},
            userShowDetail: false,
            // 审核
            showAuthCompany:false,
            authCompany:{
                id: 0,
                status : 1,
                reason: ""
            },
            formRules: {
                status: [],
                reason: []
            },
            // 全选或全不选
            getRowkeys (row) {
                return row.id
            },
            multipleSelection: [],
            selectStr: 'all',
            last_page: '',
            // 推送
            showTuisong: false,
            formRules: {
                title: [{required: true,message: "请输入标题",trigger: "blur"}],
                content: [{required: true,message: "请输入文章内容",trigger: "blur"}],
            },
            tuisongForm: {
                title: '',
                content: '',
                category: "1",
                selectStr: '1'
            },
            // 选择用户
            showChooseUser: false,
            listAllData: [],
            // 百度编辑器
            ueConfig: {
                // 初始容器高度
                initialFrameHeight: 300,
                // 初始容器宽度
                initialFrameWidth: '100%',
                // 上传文件接口
                elementPathEnable: false,
                wordCount: false,
                UEDITOR_HOME_URL: '/UEditor/',
                imageActionName: 'uploadimage', //执行上传图片的action名称,
                imageFieldName: "file", //提交的图片表单名称
                imageAllowFiles: [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
                "fileActionName": "uploadfile",
                "fileFieldName": "file",
                "fileAllowFiles": [
                    ".png", ".jpg", ".jpeg", ".gif", ".bmp",
                    ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
                    ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
                    ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
                    ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
                ],
            },
            // 用户推送列表
            userTSlistFlag: false,
            userTSlist: [],
            userTSlistDetailFlag: false,
            userTSlistDetail: {}
        };
    },
    created(){
        this.BASE_URL = BASE_URL;
        this.getList();
    },
    methods: {
        getList(page=1){
            this.tableload = true;
            apis.getList({
                ...this.search,
                page: page,
                per_page: this.limit
            }).then((data)=>{
                this.tableload = false;
                if(data.code){
                    this.$message.error(data.message);
                    return;
                }
                this.listLen = Number(data.data.total);
                this.limit = Number(data.data.per_page);
                this.listData = data.data.data;
                this.last_page = data.data.last_page;
            }).catch(()=>{
                this.tableload = false;
                this.$message({type: "error",message: "请求失败",duration: 1800});
            });
        },
        company(row){
            this.detailData = row;
            this.showAuthCompany = true;
            this.authCompany.id = row.id;
        },
        submitCompany(userId){
            this.$refs["sh"].validate(valid => {
                if (!valid) {return;}
                apis.varify(this.authCompany).then((data)=>{
                    if (data.code) {
                        this.$message({type: "error",message: data.message,duration: 1800});
                        return;
                    }
                    this.$message.success("操作成功");
                    this.showAuthCompany = false;
                    this.getList(this.currentPage);
                    this.getQyList(userId);
                }).catch(()=>{
                    this.$message({type: "error",message: "请求失败",duration: 1800});
                });
            });
        },
        toPage(val){
            this.currentPage = val;
            this.getList(val);
        },
        getQyList(id){
            apis.userCompany({user_id: id}).then((res) => {
                if(res.code == 0){
                    this.userDataList = res.data.data;
                }else{
                    this.$message.error("企业列表获取失败");
                }
            })
        },
        detail(row, num){
            if(num == 1){
                this.detailData = row;
                this.showDetail = true;
                this.getQyList(row.id);
                this.avatar = row.avatar;
            }
            if(num == 0){ // 删除
                this.$confirm("确定删除该用户吗", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then((response)=>{
                    apis.del({id: row.id}).then((data)=>{
                        if(data.code){
                            this.$message.error(data.message);
                            return;
                        }
                        this.$message.success("删除成功");
                        this.getList();
                    }).catch(()=>{
                        this.$message.error("删除失败");
                    })
                }).catch(()=>{
                    this.$message.info("取消删除");
                });
            }
        },
        // 账号下的企业查看
        userCompanyDetail(row){
            this.userDetailData = row;
            this.userShowDetail = true;
        },
        resetForm(name){
            this.$refs[name].resetFields();
        },
        hideForm(ref,val){
            this.$refs[ref].resetFields();
            this[val] = false;
        },
        // 全选与全不选
        handleSelectionChange(val) {
            let multipleSelectionAll = val;
            if(multipleSelectionAll.length == this.listLen){
                this.selectStr = 'all';
            }else{
                let selected = [];
                for(var i = 0; i < multipleSelectionAll.length; i++){
                    selected.push(multipleSelectionAll[i].id);
                }
                this.selectStr = selected.join(',')
            }
        },
        // 选择用户按钮改变
        chooseUserRadio(e){
            if(e == 1){
                this.tuisongForm.selectStr = '1';
                this.selectStr = 'all';
            }else if(e == 2){
                this.showChooseUser = true;
                this.tuisongForm.selectStr = '2';
                this.getAllList();
            }
        },
        getAllList(){
            apis.getList({
                ...this.search,
                page: 1,
                per_page: this.listLen
            }).then((data)=>{
                this.tableload = false;
                if(data.code){
                    this.$message.error(data.message);
                    return;
                }
                this.listAllData = data.data.data;
            }).catch(()=>{
                this.tableload = false;
                this.$message({type: "error",message: "请求失败",duration: 1800});
            });
        },
        quedingUser(){
            this.showChooseUser = false;
        },
        tuisongMessage(){
            this.showTuisong = true;
        },
        // 插入图片
        upimgBefore(){ this.upimgLoding = true; },
        upimgSuccess(data){
            this.upimgLoding = false;
            let quill = this.$refs.myQuillEditor.quill;
            if (data.code) {this.$message.error('图片插入失败');return false;}
            let length = quill.getSelection().index; // 获取光标所在位置
            quill.insertEmbed(length, 'image', BASE_URL + "/" + data.data.key); // 插入图片
            quill.setSelection(length + 1); // 调整光标
        },
        upimgError(){
            this.upimgLoding = false;
            this.$message.error('图片插入失败');
        },
        addTuisong(){
            console.log(this.selectStr)

            if(!this.selectStr){this.$message.error("请选择用户"); return false;}
            this.$refs['tuisongForm'].validate((valid) => {
                if (!valid) {return false;}
                apis.addTuisong({
                    title: this.tuisongForm.title,
                    content: this.tuisongForm.content,
                    category: this.tuisongForm.category,
                    scope: this.selectStr
                }).then((data)=>{
                    this.showTuisong = false;
                    if(data.code){
                        this.$message.error(data.message);
                        return;
                    }
                    this.$message.success("推送成功");
                    this.tuisongForm = {
                        title: '',
                        content: '',
                        category: "1",
                        selectStr: '1'
                    }
                }).catch(()=>{
                    this.showTuisong = false;
                    this.$message({type: "error",message: "请求失败",duration: 1800});
                });
            })
        },
        addTuisongQuxiao(){
            this.showTuisong = false;
            this.tuisongForm = {
                title: '',
                content: '',
                category: "1",
                selectStr: '1'
            }
        },
        xinxituisongList(row){
            this.userTSlistFlag = true;
            apis.getMessageByUser({
                user_id: row.id
            }).then((data)=>{
                this.userTSlist = data.data;
            }).catch(()=>{
                this.$message({type: "error",message: "请求失败",duration: 1800});
            });
        },
        xiaoxiDetail(row){ // 查看消息列表详情
            this.userTSlistDetailFlag = true;
            this.userTSlistDetail = row.message;
        },
    },
    watch:{
        authCompany:{
            deep: true,
            handler(){
                let b = this.authCompany.status==1;
                this.$set(this.formRules,"reason",b?[]:[{required: true,message: "请输入理由",trigger: "blur"}]);
            },
        },
        // 分页全选-监听数据变化
        lists: {
          handler (value) {
            if (this.allCheck) {
              this.lists.forEach(row => {
                if (row) {
                  this.$refs.table.toggleRowSelection(row, true)
                }
              });
            }
          },
          deep: true
        },
    },
    components: {
        VueUeditorWrap
    }
};
</script>

<style scoped>
.continer{padding-bottom:62px;}
.editor{height:400px; color:#333; line-height:1.42;}
.dn{display:none;}
.w_100{width:100%;}

.ml_10{margin-left:10px;}
.txt_r {text-align: right;}
.topbox {margin-bottom: 20px;}

.detailTable{font-size:16px;}
.detailTable td{padding:4px 0 22px;}
.detailTable .tr{text-align:right;padding-right:12px; padding-left:0; min-width:120px; vertical-align: top;}
.topbox{display:flex; align-items:center; justify-content: space-between;}
.topbox .el-form-item{margin-bottom:0;}

.quill_pb{padding-bottom:46px;}
.tagipt{display:inline-block; padding:0 15px; width: 114px; height:30px; line-height:1; border:1px solid #DCDFE6; border-radius:16px; color:#606266;}
.tag_txt{display:inline-block; margin-right:10px; height:30px; line-height:30px; padding:0 10px 0 15px; border-radius:16px; background:#409EFF; color:#fff;}
.tag_txt>*{vertical-align:middle;}
.tag_txt .close{cursor:pointer; padding-left:10px;}
.form-img{max-width:140px; max-height:140px;}

.detail{font-size:16px; }
.detail td{padding:10px; vertical-align: middle;}
.detail img{width:50px;height:50px;border-radius:50%;}
</style>
